-
-
Notifications
You must be signed in to change notification settings - Fork 935
IsRequired should return true even if not writable #3381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Also I'm facing the same issue when I have a virtual property using a getter: /*
* @ApiProperty(
* required=true
* )
*/
public function getSomething()
{
}This won't set the property to required due to the 4 lines of code I'm removing. |
… checking on clients
| } | ||
|
|
||
| public function testShouldReturnRequiredFalseWhenRequiredTrueIsSetButMaskedByWritableFalse() | ||
| public function testShouldReturnRequiredTrueEvenIfWritableFalseSoClientCanAssumeValueCannotBeUndefinedOnReadOperation() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should remove / rephrase this test
|
Failing tests are unrelated. |
|
I'm unsure about this, what you say makes sense, wdyt @api-platform/core-team ? |
|
@soyuka is there anything I can do (more explanations, implementation change, ...) to get some feedback and know if this branch can/will be merged in the future? Thanks! |
|
I agree with this code, I'm just afraid that such change could break some implementations... I'd like to understand why we would need |
|
Isn't the issue the assumption of nullability if the property is not required?
It only says that the property may not appear, not that it is nullable. But I think you are right to say that if a property is always normalized, then it should probably be required. |
|
This bug also leads to the wrong documentation when constructor property promotion is used. For example: <?php
namespace App\Customer\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints\NotBlank;
#[ApiResource(
collectionOperations: ['post'],
itemOperations: [],
)]
class Book
{
public function __construct(
#[NotBlank]
#[ApiProperty(identifier: true)]
protected string $name,
) {
}
public function getName(): string
{
return $this->name;
}
}Here the |
|
It's been a while since this PR was created but the issue is still actual. Especially with properties set from constructors in our use case. @soyuka what is required to continue working on this issue? Please let us know if you want a fresh PR for the current version to be created. |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
While I understand what is the logic under the code I'm removing, I believe this check is wrong because it assumes the property belongs to a schema describing a model for a write operation.
In that case, we could say the field is not required because it is not writable.
However, when describing a model for a read operation (e.g. using a group in the
normalizationContext), we do want read-only fields to be marked as required. Otherwise tooling such as code generators will assume the field is nullable.This is especially happening for all identifiers when auto-generated in the backend. They are always read-only but they are also always required (the client should not assume that the id field may be null).